<template>
    <el-container class="person">

        <el-page-header @back="goBack" :content="`${title} - 会员`">
        </el-page-header>

        <div class="person-header">
            <el-button size="small" type="primary" @click="openDrawer">添加</el-button>
        </div>

        <el-main class="person__content">
            <div ref="container" class="table-container">
                <el-table ref="table" :data="datas" style="width: 100%" align="center" header-align="center">
                    <el-table-column label="会员姓名" prop="name" center />
                    <el-table-column label="会员手机号 / 邮箱" prop="account" center />

                    <el-table-column label="是否获得证书" center >
                        <template slot-scope="scope">
                            {{  scope.row.certificateId ? '是' : '否' }}
                        </template>
                    </el-table-column>


                    <el-table-column label="操作" center width="240px">
                        <template slot-scope="scope">
                            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-main>

        <drawer-user title="添加会员" :visible.sync="drawerVisible" v-if="drawerVisible" @select="handleAssociate"></drawer-user>
    </el-container>
</template>

<script>
import drawerUser from "@/components/drawerUser"
export default {
    name: "personList",

    components: {
        drawerUser
    },

    data() {
        return {
            datas: [],
            drawerVisible: false,
        };
    },

    computed: {

        title() {
            const { title } = this.$route.query;
            return title;
        },
    },

    created() {
        this.handleDatas();
    },
    methods: {

        async handleDatas() {
            const loading = this.$loading();
            try {
                const { lessonId } = this.$route.query;
                const { data } = await this.$fetch.get("/api/topic-lesson-person/list", {
                    lessonId,
                });
                this.datas = data;
            } catch (error) {
                console.log(error);
            }
            loading.close();
        },
        refresh() {
            this.handleDatas();
        },

        openDrawer() {
            this.drawerVisible = true;
        },


        handleDelete(row) {
            this.$confirm("确定要移除此会员？", "", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            }).then(async () => {

                const { lessonId } = this.$route.query;

                const { code } = await this.$fetch.post(
                    "/api/topic-lesson-person/delete",
                    {
                        lessonId,
                        userId: row.id
                    }
                );
                if (code === 0) {
                    this.$message.success("此会员已移除");
                }
                this.refresh();
            });
        },
        async handleAssociate(multipleSelection) {

            if (multipleSelection.length === 0) {
                return this.$message.error("请选择要会员");
            }
            const { lessonId } = this.$route.query;
            const { data } = await this.$fetch.post("/api/topic-lesson-person/bind", {
                lessonId: Number(lessonId),
                userIds: multipleSelection.map((item) => item.id),
            });
            if (data && data.length) {
                this.$message.success("该会员已成功绑定到此课程");
                this.drawerVisible = false;
                this.handleDatas();
            }
        },
        goBack() {
            this.$router.go(-1);
        },
    },
};
</script>

<style scoped lang="scss">
.person {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 50px);

    .title {
        font-size: 26px;
        width: 100%;
        line-height: 42px;
        justify-content: center;
        align-items: center;
        display: flex;
    }

    .person-header {
        height: 40px;
        display: flex;
        align-items: center;
        margin-top: 30px;
    }

    .person__content {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;
        padding-bottom: 0;

        .table-container {
            flex: 1;
            overflow: hidden;
            overflow-y: auto;
        }
    }

    .pagination-container {
        display: flex;
        justify-content: center;
        padding: 20px 0;

        .el-pagination {
            padding: 20px 0;
        }
    }
}


</style>
